home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d12
/
cc02.arc
/
CRC2.C
< prev
next >
Wrap
Text File
|
1986-03-14
|
5KB
|
161 lines
/*---------------------------------------------------------------------------*/
/* High Performance CRC Computation Routine */
/* */
/* Copyright 1985 W. David Schwaderer */
/* All rights reserved */
/* */
/* Compiler used :Computer Innovations C86 V2.10A */
/* Compiler used :Microsoft/Lattice C V2.03 (CFL) */
/* */
/* Warning...this program uses bit fields! */
/* For warnings on bit field hazards see : */
/* */
/* The C Wizard's Programming Reference Handbook */
/* W. David Schwaderer */
/* John Wiley and Sons, 1985 */
/* */
/* */
/* */
/* */
/* */
/*---------------------------------------------------------------------------*/
#include <stdio.h>
#define VOID int
unsigned crc_table[256]; /* globally accessible */
main(argc, argv)
int argc;
char *argv[];
{
VOID GenTable();
unsigned GenCRC();
unsigned length, crc;
static char TstArry1[] = {'\001','\000'};
/* crc = 0x9001 */
static char TstArry2[] = {'\001','\000','\001','\220'};
/* bytewise bytewise */
/* reversed reversed */
static char TestMsg[] = "This is a test message.";
GenTable(); /* fill in the crc_table */
PrtTable(); /* display the table */
length = sizeof(TstArry1);
crc = GenCRC(length, TstArry1);
/* calculate CRC */
printf("\n\n\nTstArry1 CRC = 0x%04x", crc);
length = sizeof(TstArry2);
crc = GenCRC(length, TstArry2);
/* calculate CRC */
printf("\n\n\nTstArry2 CRC = 0x%04x", crc);
length = sizeof(TestMsg) - 1; /* avoid terminating NUL */
crc = GenCRC(length, TestMsg);
/* calculate CRC */
printf("\n\n\nText = [%s]\nCRC = 0x%04x\n\n", TestMsg, crc);
return(0);
}
VOID GenTable() /* generate the look-up table */
{
int temp;
union { int i;
struct {
unsigned :8; /* unused */
unsigned i8 :1; /* high order bit */
unsigned i7 :1;
unsigned i6 :1;
unsigned i5 :1;
unsigned i4 :1;
unsigned i3 :1;
unsigned i2 :1;
unsigned i1 :1; /* low order bit */
} Bit;
} iUn;
union { unsigned int Entry;
struct {
unsigned b16 :1; /* high order bit */
unsigned b15 :1;
unsigned b14 :1;
unsigned b13 :1;
unsigned b12 :1;
unsigned b11 :1;
unsigned b10 :1;
unsigned b9 :1;
unsigned b8 :1;
unsigned b7 :1;
unsigned b6 :1;
unsigned b5 :1;
unsigned b4 :1;
unsigned b3 :1;
unsigned b2 :1;
unsigned b1 :1; /* low order bit */
} EntryBit;
} EntryUn;
for (iUn.i = 0; iUn.i < 256; iUn.i++) {
EntryUn.Entry = 0; /* bits 2 thru 6 zeroed out now */
temp = (iUn.Bit.i7 ^ iUn.Bit.i6 ^ iUn.Bit.i5 ^ iUn.Bit.i4 ^
iUn.Bit.i3 ^ iUn.Bit.i2 ^ iUn.Bit.i1);
EntryUn.EntryBit.b16 = (iUn.Bit.i8 ^ temp);
EntryUn.EntryBit.b15 = (temp);
EntryUn.EntryBit.b14 = (iUn.Bit.i8 ^ iUn.Bit.i7);
EntryUn.EntryBit.b13 = (iUn.Bit.i7 ^ iUn.Bit.i6);
EntryUn.EntryBit.b12 = (iUn.Bit.i6 ^ iUn.Bit.i5);
EntryUn.EntryBit.b11 = (iUn.Bit.i5 ^ iUn.Bit.i4);
EntryUn.EntryBit.b10 = (iUn.Bit.i4 ^ iUn.Bit.i3);
EntryUn.EntryBit.b9 = (iUn.Bit.i3 ^ iUn.Bit.i2);
EntryUn.EntryBit.b8 = (iUn.Bit.i2 ^ iUn.Bit.i1);
EntryUn.EntryBit.b7 = (iUn.Bit.i1);
EntryUn.EntryBit.b1 = (iUn.Bit.i8 ^ temp);
crc_table[iUn.i] = EntryUn.Entry;
}
}
VOID PrtTable() /* print out the look-up table */
{
int i;
for (i = 0; i < 256; i++) {
if ( !(i % 8) )
printf("\n %02x - %04x", i, crc_table[i]);
else
printf(" %04x", crc_table[i]);
}
}
unsigned GenCRC(Length, TextPtr)
unsigned Length;
char *TextPtr;
{
int i, index;
unsigned crc;
crc = 0; /* crc starts at zero for each msg */
for (i = 0; i < Length; i++, TextPtr++) {
index = ((crc ^ *TextPtr) & 0x00FF);
crc = ((crc >> 8) & 0x00FF) ^ crc_table[index];
}
return(crc);
}